package org.Demo1;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.lang.reflect.Field;

public class SaveObj {
    public static void main(String[] args) throws ClassNotFoundException, IllegalAccessException, IOException {
        Student student=new Student("xia",21);
        saveobject(student);
    }

    private static void saveobject(Student student) throws ClassNotFoundException, IllegalAccessException, IOException {
        Class aClass = Class.forName("org.Demo1.Student");
        Field[] declaredFields = aClass.getDeclaredFields();
        BufferedWriter  bw=new BufferedWriter(new FileWriter("C:\\Users\\夏明毅\\Desktop\\java课程代码\\MyReflect\\data.text"));
        for (Field declaredField : declaredFields) {
            declaredField.setAccessible(true);
            String name = declaredField.getName();
            Object o = declaredField.get(student);
            bw.write(name+"="+o);
            bw.newLine();
        }
        bw.close();
    }
}
